package com.bkgy.modular.ltTechnologyDetermine.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.excelhandle.PlanExcelHandlerImpl;
import com.bkgy.modular.datacollect_bx.bean.JDataSave642;
import com.bkgy.modular.ltTechnologyDetermine.entity.ExportTechnologyDetermine;
import com.bkgy.modular.ltTechnologyDetermine.entity.TechnologyDetermine;
import com.bkgy.modular.ltTechnologyDetermine.entity.TechnologyDeterminepd;
import com.bkgy.modular.ltTechnologyDetermine.mapper.TechnologyDetermineMapper;
import com.bkgy.modular.ltTechnologyDetermine.service.TechnologyDetermineService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.ExcelUtilsPOI;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ResourceUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @version 1.0
 * @ClassName TechnologyDetermineServiceImpl
 * @date 2023/5/18 11:14
 */
@Service
public class TechnologyDetermineServiceImpl extends ServiceImpl<TechnologyDetermineMapper, TechnologyDetermine> implements TechnologyDetermineService {

    @Resource
    private TechnologyDetermineMapper technologyDetermineMapper;

    @Resource
    private MongoTemplate mongoTemplate;

    @Override
    public ResultData findByData(JSONObject jsonObject) {
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        Integer pageSize = jsonObject.getInteger("pageSize");

        if (pageIndex == null || pageSize == null) {
            return ResultData.ResultDataFaultSelf("分页参数没有传", null);
        }
        JSONObject condition = jsonObject.getJSONObject("condition");
        List<TechnologyDetermine> list = technologyDetermineMapper.findByPageData((pageIndex - 1) * pageSize, pageIndex * pageSize, condition);
        Integer count = technologyDetermineMapper.findByPageDataCount(condition);
        return ResultData.ResultDataSuccess(list,count);
    }

    @Override
    public ResultData findByDatapd(JSONObject jsonObject) {
        JSONObject condition = jsonObject.getJSONObject("condition");
        if (condition == null) {
            return ResultData.ResultDataFaultSelf("condition参数没有传", null);
        }
        List<TechnologyDeterminepd> list = technologyDetermineMapper.findByPageDatapd(condition);
        return ResultData.ResultDataSuccess(list);
    }

    @Override
    public ResultData findByMongDbData(JSONObject jsonObject) {
        String extid = jsonObject.getString("extid");
        if (StringUtils.isEmpty(extid)) {
            return ResultData.ResultDataFaultSelf("参数有误",-1);
        }
        Query query = new Query(Criteria.where("extid").is(extid));
        JDataSave642 jDataSave642 = mongoTemplate.findOne(query, JDataSave642.class);
        HashMap<String,List<Double>> map = new HashMap<>();
        List<Double> list1 = jDataSave642.getRow().get("c642_spm_spm_elongation_set");
        List<Double> list2 = jDataSave642.getRow().get("c642_spm_spm_elongation_act");
        List<Double> list3 = jDataSave642.getRow().get("c642_fur_speed_set");
        List<Double> list4 = jDataSave642.getRow().get("c642_fur_speed_act1");
        List<Double> list5 = jDataSave642.getRow().get("c642_tb_sf_sv");
        List<Double> list6 = jDataSave642.getRow().get("c642_tb_sf_pv");
        List<Double> list7 = jDataSave642.getRow().get("c642_tb_rcf_sv");
        List<Double> list8 = jDataSave642.getRow().get("c642_tb_rcf_pv");
        List<Double> list9 = jDataSave642.getRow().get("c642_oil_top_set");
        List<Double> list10 = jDataSave642.getRow().get("c642_oil_top_act");
        List<Double> list11 = jDataSave642.getRow().get("c642_oil_bot_set");
        List<Double> list12 = jDataSave642.getRow().get("c642_oil_bot_act");
        List<Double> list13 = jDataSave642.getRow().get("c642_ex_width_act");
        List<Double> list14 = jDataSave642.getRow().get("c642_spm_measure_thk_act");
        map.put("c642_spm_spm_elongation_set",list1);
        map.put("c642_spm_spm_elongation_act",list2);
        map.put("c642_fur_speed_set",list3);
        map.put("c642_fur_speed_act1",list4);
        map.put("c642_tb_sf_sv",list5);
        map.put("c642_tb_sf_pv",list6);
        map.put("c642_tb_rcf_sv",list7);
        map.put("c642_tb_rcf_pv",list8);
        map.put("c642_oil_top_set",list9);
        map.put("c642_oil_top_act",list10);
        map.put("c642_oil_bot_set",list11);
        map.put("c642_oil_bot_act",list12);
        map.put("c642_ex_width_act",list13);
        map.put("c642_spm_measure_thk_act",list14);
        return ResultData.ResultDataFaultSelf("查询成功",map);
    }

    @Override
    public void export(JSONObject jsonObject, HttpServletResponse response) {
        // 接收传入参数
        Integer pageSize = jsonObject.getInteger("pageSize");
        Integer pageCount = jsonObject.getInteger("pageCount");
        String fileNames = jsonObject.getString("fileName");
        JSONObject condition = jsonObject.getJSONObject("condition");
        // 创建表格数据集合
        List<TechnologyDetermine> listExcel = new ArrayList<>();// 调用service查询出数据
        List<TechnologyDetermine> data = null;

        for (int i = 1; i <= pageCount; i++) {
            // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
            data = technologyDetermineMapper.findByPageData((i - 1) * pageSize, i * pageSize, condition);
            if (!data.isEmpty() && data.size() > 0) {
                for (TechnologyDetermine item : data) {
                    listExcel.add(item);
                }
            }
        }
        if (CollectionUtils.isEmpty(listExcel)) {
            return;
        }
        List<ExportTechnologyDetermine> list = new ArrayList<>();
        for (TechnologyDetermine item : listExcel) {
            ExportTechnologyDetermine exportTechnologyDetermine = new ExportTechnologyDetermine();
            BeanUtils.copyProperties(item,exportTechnologyDetermine);
            list.add(exportTechnologyDetermine);
        }
        try {
            Map<String,Object> data1 = new HashMap();
            data1.put("data",list);
            File file = ResourceUtils.getFile("classpath:exceltemplate\\2130连退工艺判定.xlsx");
            TemplateExportParams templateExportParams = new TemplateExportParams(file.getPath(),false);
            templateExportParams.setDictHandler(new PlanExcelHandlerImpl());
            Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, data1);
            ExcelUtilsPOI.exportExcel(fileNames,response,workbook);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
